﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.TextFormatting;
using System.Windows.Shapes;
using Wpf_Examples.Tools;

namespace Wpf_Examples.Views
{
    /// <summary>
    /// ParticleWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ParticleWindow : Window
    {
        public ParticleWindow()
        {
            InitializeComponent();

            this.Loaded += MainWindow_Loaded;
        }
        private CreateParticleHelper cph;


        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Geometry geometry = CreateTextPath("C S D N", new Point(this.MyCanvas.Margin.Left, this.MyCanvas.Margin.Top), new Typeface(new FontFamily("微软雅黑"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal), 200);
            cph = new CreateParticleHelper(geometry, 
                20, //粒子最大半径，越大粒子大小对比越明显
                400,//粒子总数量 越大数量越多
                this.MyCanvas);
        }

        /// <summary>
        /// 创建文本路径
        /// </summary>
        /// <param name="word">文本字符串</param>
        /// <param name="point">显示位置</param>
        /// <param name="typeface">字体信息</param>
        /// <param name="fontSize">字体大小</param>
        /// <returns></returns>
        private Geometry CreateTextPath(string word, Point point, Typeface typeface, int fontSize)
        {
            FormattedText text = new FormattedText(
                word,   // 要显示的文本  
                new System.Globalization.CultureInfo("en-US"),  // US 英文字符集
                FlowDirection.LeftToRight,  // 文本流方向
                typeface,  // 字体  
                fontSize, // 字体大小 
                Brushes.Black); // 前景色
            Geometry geometry = text.BuildGeometry(point);
            PathGeometry path = geometry.GetFlattenedPathGeometry();
            return path;
        }
    }
}
